<?php

class Admin_users_model extends CI_Model{

	function validate(){
		$return = null;
		$this->db->where('email', $this->input->post('email', true));
		$pass = $this->input->post('password', true);
		$this->db->where('password', sha1(md5($pass)));
		$this->db->where('type', 'admin', true); // admin: 1
		$query = $this->db->get('mlme_users');
		if( $query->num_rows == 1 ){
			$user = $query->result();
			$userid = $user[0]->userid;
			$return = array('validated'=>true, 'userid'=>$userid);
		}
		return $return;
	}

	//retrieve all users data
	function listUsers(){
		return $this->db->get('mlme_users');
	}

	//to be used in pagination
	function listUsers_limited( $start_row=0, $limit=100 ){
		$qry = "SELECT * FROM mlme_users limit $start_row, $limit";
		return $this->db->query($qry);
	}

	//get the user data
	//@param id: userid
	function getUser( $id=null ){
		return $this->db->get_where('mlme_users', array('userid' => $id ));
	}

	//insert new data
	function addUser( $data ){
		$data['date_added'] = $data['date_updated'] = date('Y-m-d h:i:s');
		$data['password'] = sha1(md5($data['password']));
		//we need to unset the cpassword
		unset($data['cpassword']);
		return $this->db->insert('mlme_users', $data);
	}

	//update existing data
	function updateUser( $id, $data ){
		$data['date_updated'] = date('Y-m-d h:i:s');
		//we need to unset the cpassword
		unset($data['cpassword']);
		$this->db->where('userid', $id);
		return $this->db->update('mlme_users', $data);
	}

	//delete data
	function deleteUser( $id=array() ){
		if( empty($id) ) return false;
		$this->db->where_in( 'userid', $id );
		return $this->db->delete('mlme_users');
	}
}